package it.crosato.stage.server.model.comparison;

import it.crosato.stage.shared.objects.Multiset;
import it.crosato.stage.shared.objects.Pathway;
import it.crosato.stage.shared.objects.Reaction;

import java.util.Vector;

public class SetsModeller implements ISetsModeller {
	
	@Override
	public Multiset[][] getPathwaysModels(Pathway[][] pathways, boolean enzymes) {
		if (enzymes) {
			return getEnzymesMultisets(pathways);
		}
		else {
			return getReactionsMultisets(pathways);
		}
	}

	/**
	 * Calcola i multi-insiemi considerando le reazioni
	 * @param pathways le vie metaboliche
	 * @return i multi-insiemi di reazioni
	 */
	private Multiset[][] getReactionsMultisets(Pathway[][] pathways) {
		int nOrganisms = pathways.length;
		int nPathways = pathways[0].length;
		Multiset[][] multiset = new Multiset[nOrganisms][nPathways];
		for (int i = 0; i < nOrganisms; i++) {
			for (int j = 0; j < nPathways; j++) {
				Pathway pathway = pathways[i][j];
				Vector<String> reactions = new Vector<String>();
				for (int k = 0; k <pathway.getReactions().size(); k++) {
					reactions.add(pathway.getReactions().get(k).getId());
				}
				multiset[i][j] = new Multiset(reactions);
			}
		}
		return multiset;
	}
	
	/**
	 * Calcola i multi-insiemi considerando gli enzimi che catalizzano
	 * le reazioni
	 * @param pathways le vie metaboliche
	 * @return i multi-insiemi di enzimi
	 */
	private Multiset[][] getEnzymesMultisets(Pathway[][] pathways) {
		int nOrganisms = pathways.length;
		int nPathways = pathways[0].length;
		Multiset[][] multiset = new Multiset[nOrganisms][nPathways];
		for (int i = 0; i < nOrganisms; i++) {
			for (int j = 0; j < nPathways; j++) {
				Pathway pathway = pathways[i][j];
				Vector<Reaction> reactions = pathway.getReactions();
				Vector<String> enzymes = new Vector<String>();
				for (int k = 0; k < reactions.size(); k++) {
					enzymes.addAll(reactions.get(k).getEnzymes());
				}
				multiset[i][j] = new Multiset(enzymes);
			}
		}
		return multiset;
	}
}
